package com.dingup.provider.service.student.impl;

import com.dingup.api.Util.DateUtil;
import com.dingup.api.enums.ExamTypeEnum;
import com.dingup.api.model.student.StudentExamInformation;
import com.dingup.api.model.student.StudentSubjectInformation;
import com.dingup.provider.mapper.StudentExamInformationMapper;
import com.dingup.provider.mapper.StudentSubjectInformationMapper;
import com.dingup.provider.service.student.StudentExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Created by wancheng on 2018/8/16.
 */
@Service
public class StudentExamServiceImpl implements StudentExamService {

    @Autowired
    StudentExamInformationMapper studentExamInformationMapper;
    @Autowired
    StudentSubjectInformationMapper studentSubjectInformationMapper;

    private final String UP = "up";

    private final String DOWN = "down";

    private final String UNCHANGE = "unchange";

    @Override
    public void saveStudentExam(StudentExamInformation studentExamInformation) {
        studentExamInformation.setName(ExamTypeEnum.getDescription(studentExamInformation.getExam()));
        studentExamInformationMapper.insertSelective(studentExamInformation);
        List<StudentSubjectInformation> list = studentExamInformation.getSubjectInformations();
        list.stream().forEach(
                s -> {
                    s.setExamId(studentExamInformation.getId());
                    studentSubjectInformationMapper.insert(s);
                }
        );
    }

    @Override
    public Map<String, List<StudentExamInformation>> getStudentExam(String studentId) {
        List<StudentExamInformation> exam = getExam(studentId, false);
        List<StudentExamInformation> targetExam = getExam(studentId, true);
        Map<String, List<StudentExamInformation>> map = new HashMap<>();
        map.put("exam", exam);
        map.put("targetExam", targetExam);
        return map;
    }

    private List<StudentExamInformation> getExam(String studentId, Boolean ifTargetExam) {
        List<StudentExamInformation> exam = studentExamInformationMapper.getByUserIdAndIfTargetExam(studentId, ifTargetExam);
        Map<String,StudentExamInformation> before = new ConcurrentHashMap<>();
        Date currentDate = new Date();
        for (StudentExamInformation e : exam) {
            if (before.get(e.getExam()) != null) {
                Integer defrence = e.getTotalSource() - before.get(e.getExam()).getTotalSource();
                if (defrence < 0) {
                    e.setLiftingSource(-defrence);
                    e.setIfLifting(DOWN);
                } else if (defrence == 0) {
                    e.setLiftingSource(defrence);
                    e.setIfLifting(UNCHANGE);
                } else {
                    e.setLiftingSource(defrence);
                    e.setIfLifting(UP);
                }
                e.setNumber(before.get(e.getExam()).getNumber() + 1);
            }else {
                e.setNumber(1);
            }
            before.put(e.getExam(),e);
            List<StudentSubjectInformation> s = studentSubjectInformationMapper.getByExamId(e.getId());
            e.setSubjectInformations(s);
            e.setExamTimeString(DateUtil.date2Str(e.getExamTime(), "yyyy-MM-dd"));
            //如果是目标考试,增加是否过期属性
            if (ifTargetExam){
                if (e.getExamTime().before(currentDate)){
                    e.setIfExpire(true);
                }
            }

        }
        Collections.sort(exam, new Comparator<StudentExamInformation>() {
            @Override
            public int compare(StudentExamInformation o1, StudentExamInformation o2) {
                return o2.getNumber() - o1.getNumber();
            }
        });
        return exam;
    }

    @Override
    @Transactional
    public void deleteStudentExam(Integer id) {
        studentExamInformationMapper.deleteByPrimaryKey(id);
        studentSubjectInformationMapper.deleteByExamId(id);
    }
}
